﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Project.Interfaces;


namespace Project.Implementations.WebsiteThumbnail
{
    public class WebsiteThumbnail
    {
        protected string _url;
        protected int _width, _height;
        protected int _thumbWidth, _thumbHeight;
        protected Bitmap _bmp;

        public static void StoreThumbnail(string url)
        {
            WebsiteThumbnail thumbnail = new WebsiteThumbnail(url);
            StoreThumbnail(thumbnail);
        }

        public static void StoreThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight)
        {
            WebsiteThumbnail thumbnail = new WebsiteThumbnail(url, width, height,
              thumbWidth, thumbHeight);
            StoreThumbnail(thumbnail);
        }

        public static void StoreThumbnail(WebsiteThumbnail thumbnail)
        {
            Bitmap bmp = thumbnail.GetThumbnail();
            SaveBMP(ref bmp);
            //bmp.Dispose();
            //bmp.Save(@"C:\\example.png", System.Drawing.Imaging.ImageFormat.Png);
        }

        private static void SaveBMP(ref Bitmap bmp) // now 'ref' parameter
        {
            try
            {
                bmp.Save("C:\\example.png", System.Drawing.Imaging.ImageFormat.Png);
            }
            catch
            {
                Bitmap bitmap = new Bitmap(bmp.Width, bmp.Height, bmp.PixelFormat);
                Graphics g = Graphics.FromImage(bitmap);
                g.DrawImage(bmp, new Point(0, 0));
                g.Dispose();
                bmp.Dispose();
                bitmap.Save("C:\\example.png", System.Drawing.Imaging.ImageFormat.Png);
                bmp = bitmap; // preserve clone        
            }
        }

        public static Bitmap GetThumbnailfromFile(string url)
        {
            return (Bitmap) Bitmap.FromFile(url);
        }

        
        /// <summary>
        /// Protected constructor
        /// </summary>
        protected WebsiteThumbnail(string url)
        {
            _url = url;
            _width = 1024;
            _height = 78;
            _thumbWidth = 80;
            _thumbHeight = 80;
        }
        protected WebsiteThumbnail(string url, int width, int height, int thumbWidth,
          int thumbHeight)
        {
            _url = url;
            _width = width;
            _height = height;
            _thumbWidth = thumbWidth;
            _thumbHeight = thumbHeight;
        }

        /// <summary>
        /// Returns a thumbnail for the current member values
        /// </summary>
        /// <returns>Thumbnail bitmap</returns>
        protected Bitmap GetThumbnail()
        {
            // WebBrowser is an ActiveX control that must be run in a single-threaded
            // apartment so create a thread to create the control and generate the
            // thumbnail
            Thread thread = new Thread(new ThreadStart(GetThumbnailWorker));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return _bmp;
        }

        /// <summary>
        /// Creates a WebBrowser control to generate the thumbnail image
        /// Must be called from a single-threaded apartment
        /// </summary>
        protected void GetThumbnailWorker()
        {
            using (WebBrowser browser = new WebBrowser())
            {
                browser.ClientSize = new Size(_width, _height);
                browser.ScrollBarsEnabled = false;
                browser.ScriptErrorsSuppressed = true;
                browser.Navigate(_url);
                // Wait for control to load page
                while (browser.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
                // Render browser content to bitmap
                _bmp = new Bitmap(_thumbWidth, _thumbHeight);
                browser.DrawToBitmap(_bmp, new Rectangle(0, 0, _thumbWidth, _thumbHeight));
                //SaveBMP(ref _bmp);
            }
        }
    }
}